<template>
  <div>这是首页</div>
  <el-button type="primary" @click="onCreateWindow">创建窗口</el-button>
  <el-button type="primary" @click="onCloseWindow">关闭窗口</el-button>
  <el-button type="primary" @click="onsetWindowSize">修改窗口</el-button>
  <el-button type="primary" @click="onGetWindowList">获取窗口</el-button>
  <el-button type="primary" @click="onSendMsg">发送消息</el-button>
</template>

<script lang="ts" setup>
import { useIpc } from '@/composable/useIpc'

const ipc = useIpc()

// 创建窗口
const onCreateWindow = () => {
  ipc
    .request('/create-window', { name: 'about', url: '/about', width: 500, height: 500 })
    .then((res) => {
      console.log('创建窗口', res)
    })
    .catch((err) => {
      console.error('[渲染进程] IPC 请求失败:', err)
    })
}
// 关闭窗口
const onCloseWindow = () => {
  ipc
    .request('/close-window', { name: 'about' })
    .then((res) => {
      console.log('关闭窗口', res)
    })
    .catch((err) => {
      console.error('[渲染进程] IPC 请求失败:', err)
    })
}
// 修改窗口
const onsetWindowSize = () => {
  ipc
    .request('/set-window-size', { width: 1920, height: 1080 })
    .then((res) => {
      console.log('修改窗口', res)
    })
    .catch((err) => {
      console.error('[渲染进程] IPC 请求失败:', err)
    })
}
// 获取窗口
const onGetWindowList = () => {
  ipc
    .request('/get-window-list', { name: 'vue3' })
    .then((res) => {
      console.log('窗口列表', res)
    })
    .catch((err) => {
      console.error('[渲染进程] IPC 请求失败:', err)
    })
}

const onSendMsg = () => {
  ipc
    .request('/send-message-to-window', {
      name: 'about',
      channel: 'notify-renderer111',
      payload: '你好，我是home渲染进程'
    })
    .then((res) => {
      console.log('发送成功', res)
    })
    .catch((err) => {
      console.error('[渲染进程] IPC 请求失败:', err)
    })
}

// 监听主进程发来的消息
// ipc.on('notify-renderer', (payload) => {
//   console.log('收到主进程消息:', payload)
// })
</script>

<style scoped lang="scss">
.a {
  width: 100px;
}
</style>
